package com.bosssoft.hr.train.jsp.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.bosssoft.hr.train.jsp.example.exception.BusinessException;
import com.bosssoft.hr.train.jsp.example.service.impl.UserServiceImpl;
import com.bosssoft.hr.train.jsp.example.service.UserService;
import com.bosssoft.hr.train.jsp.example.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @param
 * @description:
 * @author: Administrator
 * @create: 2020-05-30 11:22
 * @since
 **/
@Slf4j
public class RemoveUserController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            doPost(req,resp);
        }catch (ServletException | IOException e){
            log.error(e.getMessage(),e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            JSONObject jsonObject = JSONUtil.getJson(req);
            boolean isSuccess = remove(Long.parseLong(jsonObject.getString("id")));

            JSONObject resultJSON = new JSONObject();
            resultJSON.put("isSuccess",isSuccess);
            JSONUtil.writeJson(resp,resultJSON);
        }catch (NumberFormatException | IOException e){
            log.error(e.getMessage(),e);
        }

    }

    private boolean remove(Long id){
        try {
            UserService userService = new UserServiceImpl();
            return userService.remove(id);
        }catch (BusinessException e){
            log.error(e.getMessage(),e);
        }

        return false;
    }
}
